package com.pu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pu.context.BaseContext;
import com.pu.entity.AddressBook;
import com.pu.mapper.AddressBookMapper;
import com.pu.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author Yusc
 * @Date 2023/11/1 17:03
 * @Version
 * @Description C端地址薄管理
 **/
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * @param addressBook 前端传递的数据
     * @auther: Yusc
     * @date 2023/11/1 17:26
     * @Description 根据id修改地址数据
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
    }

    /**
     * @param addressBook 前端传递的数据
     * @auther: Yusc
     * @date 2023/11/1 17:32
     * @Description 设置默认地址
     */
    @Override
    @Transactional  //开启事物
    public void setDefault(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());  //获取用户id
        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //2、将当前地址改为默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.updateById(addressBook);
    }


}
